﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Sustainalytics.ClientDW.Entities;
using Sustainalytics.Companies;
using Sustainalytics.DataLayer.Shared;
using Sustainalytics.FileManager;
using System;
using System.Collections.Generic;
using System.IO;

namespace Sustainalytics.GCCS.Tests
{
    [TestClass]
    public class GCExportManagerTests
    {
        [TestMethod]
        public void Create_Success()
        {
            var gcExportManager = CreateGCExportManager();
        }

        [TestMethod]
        public void GetPdfExport_Success()
        {
            var gcExportManager = CreateGCExportManager();
            byte[] pdfBytes = gcExportManager.GetPdfExport(new List<int> { }, Guid.NewGuid(), GCAssessment.NonCompliant, 1, false);

            Assert.IsTrue(pdfBytes != null && pdfBytes.Length > 0, "Empty PDF.");

            File.WriteAllBytes("getPdfExportSuccess.pdf", pdfBytes);
        }

        private IGCExportManager CreateGCExportManager()
        {
            List<int> universeIdsReq = new List<int> { 1, 2, 3, 4, 5 };

            GCOverallCompanySummary company;
            List<GCPrincipleSummaryExtended> principles;
            List<GCPrincipleEventIndicatorExtended> principlesIndicators;
            List<GCSource> sources;

            var imageFileManager = new DiskFileManager(@"Images\GcPdfExport");

            IRepositoryCollection<GCCurrentQuarter> currentQuarter = null;
            IGCOverallCompanySummaryManager companyManager;
            IGCPrincipleSummaryExtendedManager principleManager;
            IGCPrincipleEventIndicatorExtendedManager principleIndicatorManager;
            IGCSourceManager sourceManager;
            ICompaniesManager companiesManager;


            #region Data - Company
            {
                var companyJson = File.ReadAllText(@"Data\Company.json");
                company = JsonConvert.DeserializeObject<GCOverallCompanySummary>(companyJson);
            }
            #endregion

            #region Data - Principles
            {
                var gcPrinciplesJson = File.ReadAllText(@"Data\Principles.json");
                principles = JsonConvert.DeserializeObject<List<GCPrincipleSummaryExtended>>(gcPrinciplesJson) ?? new List<GCPrincipleSummaryExtended>();
            }
            #endregion

            #region Data - Principles Indicators
            {
                var gcPrinciplesIndicatorsJson = File.ReadAllText(@"Data\PrinciplesIndicators.json");
                principlesIndicators = JsonConvert.DeserializeObject<List<GCPrincipleEventIndicatorExtended>>(gcPrinciplesIndicatorsJson) ?? new List<GCPrincipleEventIndicatorExtended>();
            }
            #endregion

            #region Data - Sources
            {
                var sourcesJson = File.ReadAllText(@"Data\Sources.json");
                sources = JsonConvert.DeserializeObject<List<GCSource>>(sourcesJson) ?? new List<GCSource>();
            }
            #endregion

            #region Company Manager
            companyManager = new Sustainalytics.GCCS.Fakes.StubIGCOverallCompanySummaryManager
            {
                GetByCompanyListOfInt32GuidNullableOfGCAssessmentBoolean = (universeIds, companyId, overallAssessment, intersectUniverse) =>
                {
                    return company;
                }
            };
            #endregion

            #region Principle Manager
            principleManager = new Sustainalytics.GCCS.Fakes.StubIGCPrincipleSummaryExtendedManager
            {
                GetListGuidNullableOfGCAssessment = (Guid companyId, GCAssessment? overallAssessment) =>
                {
                    return principles;
                }
            };
            #endregion

            #region Principle Indicator Manager
            principleIndicatorManager = new Sustainalytics.GCCS.Fakes.StubIGCPrincipleEventIndicatorExtendedManager
            {
                GetListGuidNullableOfGCAssessment = (Guid companyId, GCAssessment? overallAssessment) =>
                    {
                        return principlesIndicators;
                    }
            };
            #endregion

            #region Sources Manager
            sourceManager = new Sustainalytics.GCCS.Fakes.StubIGCSourceManager
            {
                GetListGuidNullableOfInt32NullableOfGCAssessment = (Guid companyId, int? type, GCAssessment? eventIndicatorAssessment) =>
                    {
                        return sources;
                    }
            };
            #endregion

            #region Companies Manager

            companiesManager = new Sustainalytics.Companies.Fakes.StubICompaniesManager
            {
                GetCompanyByIdGuid = (Guid id) =>
                    {
                        return new CorporateDataExtended() { BusinessDescription = "Business Description was introduced here for pdf purposes" };
                    }
            };

            #endregion

            var gcExportManager = new GCExportManager(currentQuarter,
                imageFileManager: imageFileManager,
                overallCompanySummaryManager: companyManager,
                principleSummaryExtendedManager: principleManager,
                principleEventIndicatorExtendedManager: principleIndicatorManager,
                sourceManager: sourceManager,
                companiesManager: companiesManager
                );

            return gcExportManager;
        }
    }
}
